package com.yijiu.trb;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.resource.ProjectDetails;
import com.yijiu.response.ProjectDetailsResponse;
import com.yijiu.utils.CheckLoginStatus;
import com.yijiu.utils.ImageUtils;

public class ProjectDetailsActivity extends BTitleActivity
{
	private ProjectDetails projectDetails;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_project_details);
		requestProjectDetails();
	}

	private void findViewsInit()
	{
		titleView.setText("项目详情");
		TextView htNumTextView = (TextView) findViewById(R.id.tv_project_details_ht_num);
		TextView dbNumTextView = (TextView) findViewById(R.id.tv_project_details_dbh_num);
		TextView needMoneyTextView = (TextView) findViewById(R.id.tv_project_details_need_money);
		TextView dhWayTextView = (TextView) findViewById(R.id.tv_project_details_dh_way);
		TextView investManTextView = (TextView) findViewById(R.id.tv_project_details_invest_people);
		TextView yearGetTextView = (TextView) findViewById(R.id.tv_project_details_year_get);
		TextView statusTextView = (TextView) findViewById(R.id.tv_project_details_status);
		TextView moneyHasTimeTextView = (TextView) findViewById(R.id.tv_project_details_money_has_time);
		TextView investLowestTextView = (TextView) findViewById(R.id.tv_project_details_invest_lowest);
		TextView hasInvestTextView = (TextView) findViewById(R.id.tv_project_details_has_invest_money);
		TextView endNeedMoneyTextView = (TextView) findViewById(R.id.tv_project_details_end_need_money);
		TextView doneCondiTextView = (TextView) findViewById(R.id.tv_project_details_done_condition);
		TextView useTextView = (TextView) findViewById(R.id.tv_project_details_project_use);
		TextView sTimeTextView = (TextView) findViewById(R.id.tv_project_details_start_time);
		TextView eTimeTextView = (TextView) findViewById(R.id.tv_project_details_end_time);
		TextView statusTxtTextView = (TextView) findViewById(R.id.tv_project_details_status_text);
		TextView infoTextView = (TextView) findViewById(R.id.tv_project_details_project_info);
		TextView dbTeamTextView = (TextView) findViewById(R.id.tv_project_details_db_team);
		TextView dbWordTextView = (TextView) findViewById(R.id.tv_project_details_db_word);
		/**
		 * 担保承诺函
		 */
		TextView dbWordTitleTextView = (TextView) findViewById(R.id.project_detail_text_view);
		ImageView imageView = (ImageView) findViewById(R.id.iv_project_details_db_pic);
		/**
		 * 投资按钮
		 */
		Button btnInvest = (Button) findViewById(R.id.btn_project_details_do_invest);
		/**
		 * 检测投资按钮状态
		 */
		if (projectDetails.loanAmount.equals(projectDetails.loanedAmount))
		{
			btnInvest.setVisibility(View.INVISIBLE);
		}
		else
		{
			btnInvest.setVisibility(View.VISIBLE);
			if (CacheData.ROLE.equals("11"))
			{
				btnInvest.setVisibility(View.INVISIBLE);
			}
			else if (CacheData.ROLE.equals("12"))
			{
				btnInvest.setVisibility(View.VISIBLE);
			}
		}
		String contractNoStr = StringUtils.isNull(projectDetails.contractNo) ? "待成立" : projectDetails.contractNo;
		htNumTextView.setText("合同编号：" + contractNoStr);
		dbNumTextView.setText("担保函编号：" + projectDetails.guaranteeLicenseNo);
		dhWayTextView.setText("兑换方式：到期兑付本金及收益");
		investManTextView.setText("投资接受人：" + projectDetails.userName);
		String abcString = projectDetails.guaranteeAudit;
		if (abcString.equals(""))
		{
			dbWordTitleTextView.setText("null");
		}
		else
		{
			if (abcString.equals("IS"))
			{
				dbWordTitleTextView.setText("担保函");
			}
			else if (abcString.equals("NO"))
			{
				dbWordTitleTextView.setText("担保承诺函");
			}
		}
		/**
		 * 需求金额
		 */
		String amount1s = projectDetails.loanAmount;
		amount1s = amount1s.replace(",", "");
		double amount1d = Double.valueOf(amount1s);
		String amount2s = projectDetails.investableAmount;
		amount2s = amount2s.replace(",", "");
		double amount2d = Double.valueOf(amount2s);
		hasInvestTextView.setText("已投金额：" + Math.round(amount1d - amount2d) + "元");
		endNeedMoneyTextView.setText("还需金额：" + projectDetails.investableAmount + "元");
		dbTeamTextView.setText("担保机构：" + projectDetails.guaranteeName);
		useTextView.setText("项目用途：" + projectDetails.loanPurpose);
		dbWordTextView.setText("担保机构话语：" + projectDetails.guaranteeStatement);
		investLowestTextView.setText("最低投资：" + projectDetails.leastInvestAmount + "元");
		yearGetTextView.setText("年化收益率：" + projectDetails.interestRate);
		needMoneyTextView.setText("需求金额：" + projectDetails.loanAmount + "元");
		doneCondiTextView.setText("满标条件：" + projectDetails.saturationCondition);
		statusTextView.setText("状态：" + (projectDetails.proStatus == 1 ? "待成立" : "已成立"));
		statusTxtTextView.setText(projectDetails.proStatus == 1 ? R.string.project_status_text_one
				: R.string.project_status_text_two);
		moneyHasTimeTextView.setText("融资期限：" + projectDetails.timeLimit);
		/*------------------------------------------------------------------*/
		if (projectDetails.investAvalibleTime.equals(""))
		{
			sTimeTextView.setText("起投时间：" + "待成立");
		}
		else
		{
			sTimeTextView.setText("起投时间：" + projectDetails.investAvalibleTime);
		}
		if (projectDetails.deadline.equals(""))
		{
			eTimeTextView.setText(getIntent().getStringExtra("timeType") + "待成立");
		}
		else
		{
			eTimeTextView.setText(getIntent().getStringExtra("timeType") + projectDetails.deadline);
		}
		/*------------------------------------------------------------------*/
		infoTextView.setText(projectDetails.loanNote);
		ImageUtils.setImage(projectDetails.guaranteeLicenseUrl, imageView);
		String statusType = getIntent().getStringExtra("statusType");
		if ("2".equals(statusType) || "6".equals(statusType) || "3".equals(statusType) || "7".equals(statusType))
		{
			endNeedMoneyTextView.setVisibility(View.GONE);
			btnInvest.setVisibility(View.GONE);
			investLowestTextView.setVisibility(View.GONE);
			hasInvestTextView.setVisibility(View.GONE);
			doneCondiTextView.setVisibility(View.GONE);
		}
	}

	private void requestProjectDetails()
	{
		// 将用户输入的信息拼接成字符串发送到服务器
		String inputStr = "service=indexTradeDetail&tradeId=" + getIntent().getStringExtra("tradeId") + "&demandId="
				+ getIntent().getStringExtra("demandId");
		Log.e("result", "===============" + inputStr);
		// 用私钥进行AES加密
		encryptBase(inputStr, C.PRIVATE_KEY);
		// 将用公钥加密后的私钥、用私钥加密后的数据进行封装
		BaseAction requestData = new BaseAction(encodedData, encriptData);
		// 登录响应
		ProjectDetailsResponse projectDetailsResponse = new ProjectDetailsResponse();
		// 请求服务器
		Request request = new Request(requestData, projectDetailsResponse, ActionType.PROJECT_DETAILS);
		netManager.excute(request, this, this);
		showProgress("正在获取投资项目详情，请稍候");
	}

	/* 操作成功返回的函数 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		int type = r.getType();
		switch (type)
		{
			case ActionType.PROJECT_DETAILS:
				ProjectDetailsResponse projectDetailsResponse = (ProjectDetailsResponse) r.getResponse();
				projectDetails = projectDetailsResponse.projectDetails;
				findViewsInit();
				break;
		}
	}

	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.PROJECT_DETAILS:
				app.showMsg(errorMsg);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	public void projectDetailsClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.btn_project_details_do_invest:
				CheckLoginStatus loginStatus = new CheckLoginStatus(this);
				Intent intent = loginStatus.checkToInitIntent(DoInvestActivity.class);
				intent.putExtra("tradeId", String.valueOf(projectDetails.tradeId));
				intent.putExtra("token", projectDetails.token);
				startActivityForResult(intent, 0);
				break;
			default:
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == 0)
		{
			requestProjectDetails();
		}
	}
}
